//
//  FHEmoticonTools.swift
//  WeiBo
//
//  Created by qfh on 16/7/19.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit


let numbersOfPage = 20


class FHEmoticonTools: NSObject {

    /// 创建单例
    static let sharedEmoticonTools = FHEmoticonTools()
    
    private override init() {
        super.init()
    }
    /// 创建 表情 bundle
    private lazy var emoticonBundle: NSBundle = {
    
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: path)!
    
    }()
    // 默认表情数组
    private lazy var defaultEmoticon: [FHEmoticon] = self.loadEmoticonsWithPath("default/info.plist")
    // emoji 数组
    private lazy var emojiEmoticon: [FHEmoticon] = self.loadEmoticonsWithPath("emoji/info.plist")
   // 浪小花 数组
    private lazy var lxhEmoticon: [FHEmoticon] = self.loadEmoticonsWithPath("lxh/info.plist")
    
    
    
     lazy var allEmoticonArray:[[[FHEmoticon]]] = {
    
        return [
        
            self.pagesWithEmotions(self.defaultEmoticon),
            self.pagesWithEmotions(self.emojiEmoticon),
            self.pagesWithEmotions(self.lxhEmoticon)

        
        ]
    
    
    
    }()
    
    
    
    /**
     读取bundle 里表情数组
     
     - parameter subpath: <#subpath description#>
     
     - returns: <#return value description#>
     */
    private func loadEmoticonsWithPath(subpath: String) -> [FHEmoticon] {
    
    
        var emoticonArray = [FHEmoticon]()
    
        let path = self.emoticonBundle.pathForResource(subpath, ofType: nil)!
        
        let emoticonDictArray = NSArray(contentsOfFile: path)
        
        for dict in emoticonDictArray as! [[String : AnyObject]] {
        
            let emoticon = FHEmoticon(dic: dict)
            
            if emoticon.type == "0" {
            
                let lastPath = (path as NSString).stringByDeletingLastPathComponent
                emoticon.path = lastPath + "/" + emoticon.png!
 
                
            }
            
            emoticonArray.append(emoticon)
        
        }
    
    
        return emoticonArray
    }
    
    
    
    /**
    *  讲数组 转换为 二维数组
    */
    
    
    private func pagesWithEmotions(emoticon: [FHEmoticon]) -> [[FHEmoticon]] {
    
        var emoticonArray = [[FHEmoticon]]()
        
        let page = (emoticon.count - 1) / numbersOfPage + 1
        
        
        for i in 0..<page  {
                    
            
            let loc = i * numbersOfPage
            
            var  len = numbersOfPage
            
            if loc + len > emoticon.count {
                
                len = emoticon.count - loc
                
            }
            
            
            let subArray = (emoticon as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [FHEmoticon]
            
        
            emoticonArray.append(subArray)
            
        }
        
    
        return emoticonArray
    }
    
    
}
